﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DataAccess;
using SPIWareMVVM.ViewModels;

namespace SPIWareMVVM.Views
{
    /// <summary>
    /// Interaction logic for CustomerList.xaml
    /// </summary>
    public partial class CustomerList : UserControl
    {
        Window1 mainWindow { get; set; }
        ApplicationViewModel applicationViewModel { get; set; }
        CustomersViewModel baseViewModel { get; set; }

        public Customer SelectedCustomer { get { return applicationViewModel.SelectedCustomer; } set { applicationViewModel.SelectedCustomer = value; } }
        public CustomerList()
        {
            InitializeComponent();
        }

        protected void Customers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            mainWindow = (Window1)Window.GetWindow((ListBox)sender);

            List<Type> types = new List<Type> { typeof(Customer) };
            ApplicationData.ClearAllExcept(types);
            mainWindow.HideLists();

            if (mainWindow != null)
                applicationViewModel = (ApplicationViewModel)((ObjectDataProvider)mainWindow.Resources["applicationVM"]).ObjectInstance;

            if (applicationViewModel != null)
            {
                if (e.RemovedItems.Count > 0)
                {
                    applicationViewModel.SelectedCustomer = null;
                    mainWindow.btnCustomerWO.IsEnabled = false;
                    mainWindow.btnCustomerParts.IsEnabled = false;
                    mainWindow.btnNewContact.IsEnabled = false;
                }
                if (e.AddedItems.Count > 0)
                {
                    SelectedCustomer = (Customer)e.AddedItems[0];
                    lbContactData.ItemsSource = SelectedCustomer.ocContactInfos;
                    if (SelectedCustomer.ocContactInfos.Count == 1) lbContactData.SelectedIndex = 0;
                }
                //mainWindow.customerEdit.DataContext = applicationViewModel.SelectedCustomer;

                ////mainWindow.customerEdit.lbContactData.DataContext = null;
                ////mainWindow.customerEdit.lbContactData.DataContext = SelectedCustomer.ocContactInfos;

                //if (mainWindow.customerEdit.lbContactData.Items.Count == 1)
                //    mainWindow.customerEdit.lbContactData.SelectedIndex = 0;

                mainWindow.customersList.Visibility = Visibility.Visible;
                //mainWindow.customerEdit.Visibility = Visibility.Visible;
                this.dpCustomerEdit.Visibility = Visibility.Visible;
                mainWindow.btnCustomerWO.IsEnabled = true;
                mainWindow.btnCustomerParts.IsEnabled = true;
                mainWindow.btnNewContact.IsEnabled = true;
            }
        }
        protected void Address_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.RemovedItems.Count > 0)
                dpAddressEdit.Visibility = Visibility.Collapsed;
            if (e.AddedItems.Count > 0)
                dpAddressEdit.Visibility = Visibility.Visible;
        }
    }
}
